<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/2/20
 * Time: 20:35
 */

namespace app\admin\controller;


use think\Db;
use think\facade\Request;
use app\admin\model\User as UserModel;
use app\admin\validate\User as UserValidate;
class User extends Common
{
    /**
     * 用户列表
     * @return array|\think\response\View
     * @throws \think\exception\DbException
     */
    public function userList()
    {
        if (Request::isAjax()) {
            $username = trim(input("username"));
            $status = trim(input("status"))?trim(input("status")):"";

            // where条件
            $where = [];
            $where[] = ["username","like",'%'.$username.'%'];
            if($status != '')
            {
                $where[] = ["status","=",$status];
            }

            //获取分页参数
            $pageOption = $this->getPageAndPageSize(input('page'),input('limit'));

            //获取列表
            $list = UserModel::getPageListByPage($pageOption['page'],$pageOption['pageSize'],$where,['create_time'=>'desc'],'id,username,status');
            return $result = ['code' => 0, 'msg' => '获取成功!', 'data' => $list['data'], 'count' => $list['total'], 'rel' => 1];
        }
        return view("user_list");
    }

    /**
     * 添加用户
     * @return array|\think\response\View
     */
    public function userAdd()
    {
        if (Request::isAjax()) {
            $data = input("post.");

            //验证
            (new UserValidate())->check($data);

            //添加
            $r = UserModel::create($data);
            if($r !== false)
            {
                return $result = ["code"=>0,"msg"=>"添加成功","data"=>[]];
            }
            else{
                return $result = ["code"=>1,"msg"=>"添加失败","data"=>[]];
            }
        }
        return view("user_add");
    }

    /**
     * 编辑用户
     * @return array|\think\response\View
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function userEdit()
    {
        if (Request::isAjax()) {
            $data = input("post.");

            $id = $data['id'];
            unset($data['id']);

            //验证
            (new UserValidate())->check($data);

            //添加
            $where[] = ["id","=",$id];
            $r = UserModel::updateOneData($where,$data);
            if($r !== false)
            {
                return $result = ["code"=>0,"msg"=>"编辑成功","data"=>[]];
            }
            else{
                return $result = ["code"=>1,"msg"=>"编辑失败","data"=>[]];
            }
        }
        $id = trim(input("id"));
        $where[] = ["id","=",$id];
        $info = UserModel::getFieldValue("*",$where);
        $this->assign("info",$info);
        return view("user_edit");
    }

    /**
     * 删除用户
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function userDel()
    {
        if (Request::isAjax()) {
            $id = trim(input("id"));

            //更新is_del
            $r = UserModel::softDel($id,'status',3);
            if($r !== false)
            {
                return $result = ["code"=>0,"msg"=>"删除成功","data"=>[]];
            }
            else{
                return $result = ["code"=>1,"msg"=>"删除失败","data"=>[]];
            }
        }
    }

    /**
     * 冻结账户
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function userStatus()
    {
        if (Request::isAjax()) {
            $id = trim(input("id"));
            $status = trim(input("status"));

            //更新is_del
            $where[] = ["id","=",$id];
            $r = UserModel::updateOneData($where,["update_time" => time(),'status'=>$status]);
            if($r !== false)
            {
                return $result = ["code"=>0,"msg"=>"冻结成功","data"=>[]];
            }
            else{
                return $result = ["code"=>1,"msg"=>"冻结失败","data"=>[]];
            }
        }
    }
}